package phone

import (
	"fmt"
	"github.com/google/wire"
	"github.com/uber/tchannel-go/thrift"
	"go.didapinche.com/goapi/outcall_thrift_service_api/v2"
	"go.uber.org/zap"
	"time"
)

const (
	InstanceId  = "155913f6-5312-48a6-b442-b726aea3d2c8"
	ScriptId    = "e631cc5a-34aa-454c-8064-0449cf2f4042"
	OutCallType = "alarm"
)

type PhoneService struct {
	logger  *zap.Logger
	service outcall_thrift_service_api.TChanOutcallThriftService
}

func NewPhoneService(service outcall_thrift_service_api.TChanOutcallThriftService, logger *zap.Logger) *PhoneService {
	return &PhoneService{
		service: service,
		logger:  logger,
	}
}

// Send 发送接口
func (s *PhoneService) Send(to string) {
	defer func() {
		if r := recover(); r != nil {
			s.logger.Error("recover RefreshTemplate")
			fmt.Println(r)
		}
	}()
	context, _ := thrift.NewContext(10 * time.Second)

	//result, err := s.service.OutBound(context, to, InstanceId, ScriptId, params, temp)
	result, err := s.service.OutCall(context, to, OutCallType, nil)

	if err != nil {
		s.logger.Error("phone send err", zap.Error(err))
		return
	}

	s.logger.Error("phone result=" + *result.Msg)
}

func (s *PhoneService) Sends(to []string) {
	for _, t := range to {
		go func() {
			s.Send(t)
		}()

	}
}

var ProviderSet = wire.NewSet(NewPhoneService)
